package com.hzy.oms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hzy.oms.domain.bo.WechatPaymentHistoryBo;
import com.hzy.oms.domain.vo.WechatPaymentHistoryVo;
import com.hzy.oms.domain.WechatPaymentHistory;
import com.hzy.oms.mapper.WechatPaymentHistoryMapper;
import com.hzy.oms.service.IWechatPaymentHistoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 微信订单Service业务层处理
 *
 * @author zengxin
 * @date 2023-10-18
 */
@RequiredArgsConstructor
@Service
public class WechatPaymentHistoryServiceImpl implements IWechatPaymentHistoryService {

    private final WechatPaymentHistoryMapper baseMapper;

    /**
     * 查询微信订单
     */
    @Override
    public WechatPaymentHistoryVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询微信订单列表
     */
    @Override
    public TableDataInfo<WechatPaymentHistoryVo> queryPageList(WechatPaymentHistoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WechatPaymentHistory> lqw = buildQueryWrapper(bo);
        Page<WechatPaymentHistoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询微信订单列表
     */
    @Override
    public List<WechatPaymentHistoryVo> queryList(WechatPaymentHistoryBo bo) {
        LambdaQueryWrapper<WechatPaymentHistory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WechatPaymentHistory> buildQueryWrapper(WechatPaymentHistoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WechatPaymentHistory> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getPaymentId()), WechatPaymentHistory::getPaymentId, bo.getPaymentId());
        lqw.eq(bo.getMemberId() != null, WechatPaymentHistory::getMemberId, bo.getMemberId());
        lqw.eq(StringUtils.isNotBlank(bo.getOpenid()), WechatPaymentHistory::getOpenid, bo.getOpenid());
        lqw.like(StringUtils.isNotBlank(bo.getRealName()), WechatPaymentHistory::getRealName, bo.getRealName());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), WechatPaymentHistory::getTitle, bo.getTitle());
        lqw.eq(bo.getOrderId() != null, WechatPaymentHistory::getOrderId, bo.getOrderId());
        lqw.eq(bo.getMoney() != null, WechatPaymentHistory::getMoney, bo.getMoney());
        lqw.eq(bo.getOpType() != null, WechatPaymentHistory::getOpType, bo.getOpType());
        lqw.eq(bo.getPaymentStatus() != null, WechatPaymentHistory::getPaymentStatus, bo.getPaymentStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getAttach()), WechatPaymentHistory::getAttach, bo.getAttach());
        lqw.eq(StringUtils.isNotBlank(bo.getResponseBody()), WechatPaymentHistory::getResponseBody, bo.getResponseBody());
        return lqw;
    }

    /**
     * 新增微信订单
     */
    @Override
    public Boolean insertByBo(WechatPaymentHistoryBo bo) {
        WechatPaymentHistory add = BeanUtil.toBean(bo, WechatPaymentHistory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改微信订单
     */
    @Override
    public Boolean updateByBo(WechatPaymentHistoryBo bo) {
        WechatPaymentHistory update = BeanUtil.toBean(bo, WechatPaymentHistory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WechatPaymentHistory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除微信订单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
